﻿//////////////////////////////////////////////////////////////////////////////
//
//  Copyright © 1998-2024 Glodon Company Limited.  All rights reserved.
//
//  Use of this software is subject to the terms of the Glodon license
//  agreement provided at the time of installation or download, or which
//  otherwise accompanies this software in either electronic or hard copy form.  
//
//////////////////////////////////////////////////////////////////////////////


#pragma once
#include "GnufItemEvents.h"
#include "GnufPallet.h"

namespace gnuf
{
    class Toolbar;
    class QtApplicationVersionDlg;
    class ButtonAppearanceSetting;
    class Label;
    class PushButton;

    class GNUF_BASE_EXPORT ApplicationVersionDlg final
        : public IItemEventHandler
        , public IPalletEventHandler
    {
    public:
        explicit ApplicationVersionDlg();
        virtual ~ApplicationVersionDlg();
        static ApplicationVersionDlg* get();

    public:
        void show(bool synchronously = true);
        void close();
        bool visible();
        Toolbar* toolBar() { return m_toolbar.get(); }
        void setApplicationInfo(const Info*);
        const Info* applicationInfo() { return m_applicationInfo.get(); }

        void  setCurrentVersion(const std::wstring& version) { m_currentVersion = version; }
        const std::wstring& getCurrentVersion() { return m_currentVersion; }

        void setlatestVersion(const std::wstring& version);
        const std::wstring& getLatestVersion() { return m_latestVersion; }

        void  setApplicationName(const std::wstring& name) { m_applicationName = name; }
        const std::wstring& getApplicationName() { return m_applicationName; }

        void setIsLatestVersion(bool isLatest);
        bool isLatestVersion() { return m_isLatestVersion; }

        typedef std::function<void()> OnCallback;
        template<typename T>
        static OnCallback CreateOnCallback(void(T::* func)(), T* object)
        {
            return std::bind(func, object);
        }
        void setTriggerQtLicenceCallback(OnCallback callback) { m_triggerQtLicenceCallBack = callback; }
        void setFinishUpdateCallback(OnCallback callback) { m_finishUpdateCallBack = callback; }
        void setUpdateCallback(OnCallback callback) { m_updateCallBack = callback; }
    protected:
        //Inherits from IItemEventHandler
        virtual void onClicked(Item *) override;
        virtual void onRefreshStyle() override;
    private:
        void initialize();
        gcmp::OwnerPtr<ButtonAppearanceSetting> customizeApplicationButtonSetting();
        gcmp::OwnerPtr<ButtonAppearanceSetting> customizeQtVersionButtonSetting();
        gcmp::OwnerPtr<ButtonAppearanceSetting> customizeButtonSetting();
    private:
        Label* m_currentVersionText = nullptr;
        Label* m_latestVersionText = nullptr;
        Label* m_auxiliaryText = nullptr;
        Label* m_companyName = nullptr;
        PushButton* m_qtVersionBtn = nullptr;
        PushButton* m_updateBtn = nullptr;
        PushButton* m_okBtn = nullptr;
        PushButton* m_applicatinBtn = nullptr;
        gcmp::OwnerPtr<QtApplicationVersionDlg> m_qtApplicationVersionDlg = nullptr;
        gcmp::OwnerPtr<Info> m_applicationInfo = nullptr;
        gcmp::OwnerPtr<Toolbar> m_toolbar = nullptr;
        gcmp::OwnerPtr<Pallet> m_pallet = nullptr;

        std::wstring m_currentVersion;
        std::wstring m_latestVersion;
        std::wstring m_applicationName;
        OnCallback m_triggerQtLicenceCallBack = nullptr;
        OnCallback m_getVersionCallBack = nullptr;
        OnCallback m_finishUpdateCallBack = nullptr;
        OnCallback m_updateCallBack = nullptr;
        bool m_isLatestVersion = true;
        friend class QtApplicationVersionDlg;
    };
}
